package com.central.apps.controller;

import cn.hutool.core.map.MapUtil;
import com.alibaba.fastjson.JSON;
import com.central.app.service.HandlerService;
import com.central.common.web.Result;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.collections.MapUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author JHY
 */
@Slf4j
@Api(tags = "合同看板接口")
@CrossOrigin
@RestController
@RequestMapping("/board")
public class ContractBoardController {
    @Autowired
    private HandlerService handlerService;

    /*-------------------------------------------------------- 合同看板 --------------------------------------------------------*/

    /*
    * 合同类型统计(饼状图)
    * */
    @GetMapping("/contypeInfo")
    public Result<?> contypeInfo(){

        List<Map<String,Object>> list = handlerService.findBySql("select contracttype as name,count(id) as value from gq_conaccount gc group by contracttype having contracttype is not null");

        return Result.succeedWith(list, 0, "查询成功");
    }
    /*
    * 合同状态统计（饼状图）
    * */
    @GetMapping("/conStatusInfo")
    public Result<?> conStatusInfo(){

        List<Map<String,Object>> list = handlerService.findBySql("select constatus as name,count(id) as value from gq_conaccount gc group by constatus having constatus is not null");

        return Result.succeedWith(list, 0, "查询成功");
    }
    /*
    * 合同数量统计，每月 签订数量（折线图）
    * */
    @GetMapping("/conSingsInfoFoldLine")
    public Result<?> conSingsInfoFoldLine(){

        List<Map<String,Object>> list = handlerService.findBySql("select date_format(createtime,'%Y-%m')as name,count(id) as value  from gq_consigns \n" +
                "group by name having name >=(select CONCAT(YEAR(CURDATE()),'-','01'))");
        Map<String,Object[]> map = new HashMap<>();
        if(CollectionUtils.isNotEmpty(list)){
            List<String> x = new ArrayList<>();
            List<String> y = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : list) {
              x.add(MapUtils.getString(stringObjectMap,"name"));
              y.add(MapUtils.getString(stringObjectMap,"value"));
              //y.addAll(stringObjectMap.());
            }
            Object[] xArray = (Object[]) x.toArray();
            Object[] yArray = (Object[]) y.toArray();
            map.put("x",xArray);
            map.put("y",yArray);
        }
        return Result.succeedWith(map, 0, "查询成功");
    }
    /*
    * 合同签订数据 列表
    * */
    @GetMapping("/conSignsInfo")
    public Result<?> conSignsInfo(){

        List<Map<String,Object>> list = handlerService.findBySql("select createtime,conname,subjectno from gq_consigns gc" );

        return Result.succeedWith(list, 0, "查询成功");
    }
    /*
     * 供应商信息
     * */
    @GetMapping("/conOppcompanyInfo")
    public Result<?> conOppcompanyInfo(){

        List<Map<String,Object>> list = handlerService.findBySql("select oppcompany ,count(id) as counts  from gq_conaccount gc group by oppcompany having oppcompany  is not null order by counts desc limit 10" );

        return Result.succeedWith(list, 0, "查询成功");
    }
    /*
     * 合同类型金额信息
     * */
    @GetMapping("/conTypeMoneyInfo")
    public Result<?> conTypeMoneyInfo(){

        List<Map<String,Object>> list = handlerService.findBySql("select contracttype as name,sum(totalprice) as value from gq_conaccount gc group by contracttype \n" +
                "having contracttype in ('服务采购合同','物资采购合同','生产外协合同','科研外协合同','其他合同')");

        return Result.succeedWith(list, 0, "查询成功");
    }
    /**
     * 系统合同状态全揽
     */
    @GetMapping("statusEverything")
    public Result<?> statusEverything() {
        // 合同状态：起草中，审批中，已批准，已签订，已生效，执行中，（变更中）待验收，验收完成，关闭，（终止）
        Map<String, Object> rsp = new HashMap<>(16);
        // 起草中
        int draft = 0;
        // 审批中
        int accra = 0;
        // 已批准
        int approval = 0;
        // 已生效
        int takeEffect = 0;
        // 执行中
        int perform = 0;
        // 待验收
        int acceptance = 0;
        // 验收完成
        int complete = 0;
        // 关闭
        int close = 0;
        List<Map<String, Object>> consignList = handlerService.findBySql("SELECT `constatus`,count(id) as num FROM `gq_consign` GROUP BY `constatus`;");
        for (Map<String, Object> item : consignList) {
            String constatus = MapUtil.getStr(item, "constatus");
            Integer num = MapUtil.getInt(item, "num");
            if (StringUtils.isNotBlank(constatus)) {
                if ("起草".equals(constatus)) {
                    draft = num;
                } else if ("审批中".equals(constatus)) {
                    accra = num;
                } else if ("已批准".equals(constatus)) {
                    approval = num;
                } else if ("已生效".equals(constatus)) {
                    takeEffect = num;
                } else if ("执行中".equals(constatus)) {
                    perform = num;
                } else if ("待验收".equals(constatus)) {
                    acceptance = num;
                } else if ("验收完成".equals(constatus)) {
                    complete = num;
                } else if ("关闭".equals(constatus)) {
                    close = num;
                }
            }
        }
        rsp.put("draft", draft);
        rsp.put("accra", accra);
        rsp.put("approval", approval);
        rsp.put("takeEffect", takeEffect);
        rsp.put("perform", perform);
        rsp.put("acceptance", acceptance);
        rsp.put("complete", complete);
        rsp.put("close", close);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /**
     * 收入合同-金额
     */
    @GetMapping("/incomeContractSum")
    public Result<?> incomeContractSum() {
        Map<String, Object> rsp = new HashMap<>(16);
        // 在执行
        int overPerform = 9000;
        // 取消
        int cancel = 1000;
        // 已完成
        int finished = 4000;

        rsp.put("overPerform", overPerform);
        rsp.put("cancel", cancel);
        rsp.put("finished", finished);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /**
     * 承揽项目金额
     */
    @GetMapping("/undertakeProjectSum")
    public Result<?> undertakeProjectSum() {
        Map<String, Object> rsp = new HashMap<>(16);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /*------------------------------------------------------ 合同工作台 ----------------------------------------------------------*/

    /**
     * 我的待办丶已处理丶已发起
     */
    @GetMapping("/myToDoOrProcessed")
    public Result<?> myToDoOrProcessed() {
        Map<String, Object> rsp = new HashMap<>(16);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /**
     * 常用应用
     */
    @GetMapping("/commonApplication")
    public Result<?> commonApplication() {
        Map<String, Object> rsp = new HashMap<>(16);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /**
     * 我的合同任务
     */
    @GetMapping("/myContractTask")
    public Result<?> myContractTask() {

        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> m1 = new HashMap<>(16);
        m1.put("bizNum", "100001");
        m1.put("taskName", "纵向001");
        m1.put("principal", "张某");
        m1.put("myRole", "节点负责人");
        m1.put("bizStatus", "红");
        Map<String, Object> m2 = new HashMap<>(16);
        m2.put("bizNum", "100002");
        m2.put("taskName", "纵向002");
        m2.put("principal", "张某");
        m2.put("myRole", "节点负责人");
        m2.put("bizStatus", "绿");
        Map<String, Object> m3 = new HashMap<>(16);
        m3.put("bizNum", "100003");
        m3.put("taskName", "纵向003");
        m3.put("principal", "张某");
        m3.put("myRole", "节点负责人");
        m3.put("bizStatus", "黄");
        Map<String, Object> m4 = new HashMap<>(16);
        m4.put("bizNum", "100001");
        m4.put("taskName", "纵向001");
        m4.put("principal", "张某");
        m4.put("myRole", "节点负责人");
        m4.put("bizStatus", "红");
        Map<String, Object> m5 = new HashMap<>(16);
        m5.put("bizNum", "100002");
        m5.put("taskName", "纵向002");
        m5.put("principal", "张某");
        m5.put("myRole", "节点负责人");
        m5.put("bizStatus", "绿");
        Map<String, Object> m6 = new HashMap<>(16);
        m6.put("bizNum", "100003");
        m6.put("taskName", "纵向003");
        m6.put("principal", "张某");
        m6.put("myRole", "节点负责人");
        m6.put("bizStatus", "黄");
        list.add(m1);
        list.add(m2);
        list.add(m3);
        list.add(m4);
        list.add(m5);
        list.add(m6);
        return Result.succeedWith(list, 200, "查询成功");
    }

    /**
     * 负责人类型
     */
    @GetMapping("/principalType")
    public Result<?> principalType(String type) {
        Map<String, Object> rsp = new HashMap<>(16);
        return Result.succeedWith(rsp, 200, "查询成功");
    }

    /*------------------------------------------------------ 项目看板 - 项目分析 ----------------------------------------------------------*/

    /**
     * 项目类型比例
     */
    @GetMapping("/projectTypeRatio")
    public Result<?> projectTypeRatio() {
        List<Map<String, Object>> data = handlerService.
                findBySql("SELECT prosource,COUNT(id) as count FROM `gq_establishproject` GROUP BY prosource;");
        return Result.succeedWith(data, 200, "查询成功");
    }

    /**
     * 项目状态
     */
    @GetMapping("/projectStatus")
    public Result<?> projectStatus() {
        List<Map<String, Object>> data = handlerService.
                findBySql("SELECT `status`,COUNT(id) as count FROM `gq_establishproject` GROUP BY `status`;");
        return Result.succeedWith(data, 200, "查询成功");
    }

    /**
     * 风险清单
     */
    @GetMapping("/riskList")
    public Result<?> riskList() {
        List<Map<String, Object>> data = handlerService.
                findBySql("SELECT proname,prostatus,riskrank FROM `gq_proaccount`;");
        return Result.succeedWith(data, 200, "查询成功");
    }

    /**
     * 项目排名
     */
    @GetMapping("/projectRanking")
    public Result<?> projectRanking() {
        List<Map<String, Object>> data = handlerService.
                findBySql("SELECT proname,prostatus,riskrank FROM `gq_proaccount`;");
        return Result.succeedWith(data, 200, "查询成功");
    }

    /*------------------------------------------------------ 项目看板 - 课题分析 ----------------------------------------------------------*/
}
